/*
 * Copyright (C) 2009 Rob Cash
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * ScheduleChooserWizardPanel.java
 * 
 * Created on Jan 3, 2009 at 12:31:23 PM
 */

package org.robcash.apps.messagepopup.view.wizard;


import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.robcash.apps.messagepopup.view.components.ScheduleChooser;
import org.robcash.apps.messagepopup.vo.Schedule;
import org.robcash.commons.swing.wizard.Wizard;
import org.robcash.commons.swing.wizard.impl.BaseWizardPanel;

/**
 *
 * @author Rob
 */
public class ScheduleChooserWizardPanel extends BaseWizardPanel<ScheduleChooser>
        implements ChangeListener {

    private ScheduleChooser scheduleChooser;

    public ScheduleChooserWizardPanel(String id) {
        super(id);
        scheduleChooser = new ScheduleChooser();
        scheduleChooser.addChangeListener(this);
        super.setComponent(scheduleChooser);
    }

    public void stateChanged(ChangeEvent e) {
        if ( scheduleChooser.equals(e.getSource()) ) {
            // Force wizard to reevaluate button states
            fireChangeListenerStateChanged(new ChangeEvent(this));
        }
    }

    @Override
    public void aboutToDisplay(Wizard wizard) {
        scheduleChooser.updateSchedules();
    }

    @Override
    public void aboutToHide(Wizard wizard, boolean saveWork) {
        if ( saveWork ) {
            Schedule schedule = scheduleChooser.getSelectedSchedule();
            if ( schedule != null ) {
                SubscriptionModel model = (SubscriptionModel) wizard.getModel();
                model.setSchedule(schedule);
                model.setScheduleIsNew(false);
            }
        }
    }

    @Override
    public boolean canGoForward() {
        return true;
    }

    @Override
    public String getNextPanelId() {
        if ( scheduleChooser.getSelectedSchedule() == null ) {
            return SubscriptionModel.SCHEDULE_EDITOR;
        }
        else {
            return SubscriptionModel.SCHEDULE_NAME;
        }
    }

}
